﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InBaoCao;

namespace QLVatTu.BaoCao
{
    public partial class frmBaoCaoNhapKho : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmBaoCaoNhapKho()
        {
            InitializeComponent();
        }

        private void loadData()
        {
            cmbKhoNhap.DataSource = cn.getTable("SELECT [MaKho],[TenKho] FROM [tbl_Kho]");
            cmbKhoNhap.DisplayMember = "TenKho";
            cmbKhoNhap.ValueMember = "MaKho";

            cmbKhachHang.DataSource = cn.getTable("SELECT [MaDoiTac],[TenDoiTac] FROM [tbl_DoiTac] WHERE [DonViSanXuat] = 1");
            cmbKhachHang.DisplayMember = "TenDoiTac";
            cmbKhachHang.ValueMember = "MaDoiTac";

            cmbCongTrinh.DataSource = cn.getTable("SELECT [MaCongTrinhDuAn],[TenCongTrinhDuAn] FROM [tbl_CongTrinhDuAn]");
            cmbCongTrinh.DisplayMember = "TenCongTrinhDuAn";
            cmbCongTrinh.ValueMember = "MaCongTrinhDuAn";

            cmbNhaCungCap.DataSource = cn.getTable("SELECT [MaDoiTac],[TenDoiTac] FROM [tbl_DoiTac] WHERE [NhaCungCap] = 1");
            cmbNhaCungCap.DisplayMember = "TenDoiTac";
            cmbNhaCungCap.ValueMember = "MaDoiTac";

            load_grid_view();
        }

        private void frmBaoCaoNhapKho_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
            else
            {
                MessageBox.Show("Kết nối CSDL thất bại!");
                this.Close();
            }
        }

        private void load_grid_view(string dieuKien = null)
        {
            string sql = "";
            if (dieuKien != null)
            {
                sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_PhieuNhapKho.SoPhieu) as STT,dbo.tbl_PhieuNhapKho.SoPhieu as PhieuNhap, dbo.tbl_PhieuNhapKho.NgayNhap as NgayLap, SUM(dbo.tbl_ChiTietPhieuNhap.SoLuong) as SoLuong, dbo.tbl_LoaiPhieu.TenLoaiPhieu as KieuNhap
                    FROM         dbo.tbl_PhieuNhapKho INNER JOIN
                                          dbo.tbl_ChiTietPhieuNhap ON dbo.tbl_PhieuNhapKho.SoPhieu = dbo.tbl_ChiTietPhieuNhap.SoPhieu INNER JOIN
                                          dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuNhapKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu INNER JOIN
                                          dbo.tbl_Kho ON dbo.tbl_PhieuNhapKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                          dbo.tbl_DoiTac ON dbo.tbl_PhieuNhapKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                          dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuNhapKho.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn
                    "
                    + dieuKien + @"
                    GROUP BY dbo.tbl_PhieuNhapKho.SoPhieu, dbo.tbl_PhieuNhapKho.NgayNhap, dbo.tbl_LoaiPhieu.TenLoaiPhieu
                ";
            }
            else
            {
                sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_PhieuNhapKho.SoPhieu) as STT,dbo.tbl_PhieuNhapKho.SoPhieu as PhieuNhap, dbo.tbl_PhieuNhapKho.NgayNhap as NgayLap, SUM(dbo.tbl_ChiTietPhieuNhap.SoLuong) as SoLuong, dbo.tbl_LoaiPhieu.TenLoaiPhieu as KieuNhap
                    FROM         dbo.tbl_PhieuNhapKho INNER JOIN
                                          dbo.tbl_ChiTietPhieuNhap ON dbo.tbl_PhieuNhapKho.SoPhieu = dbo.tbl_ChiTietPhieuNhap.SoPhieu INNER JOIN
                                          dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuNhapKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu
                    WHERE dbo.tbl_PhieuNhapKho.XacNhan = 1
                    GROUP BY dbo.tbl_PhieuNhapKho.SoPhieu, dbo.tbl_PhieuNhapKho.NgayNhap, dbo.tbl_LoaiPhieu.TenLoaiPhieu
                ";
            }
            dgvPhieuNhap.DataSource = cn.getTable(sql);
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "WHERE dbo.tbl_PhieuNhapKho.XacNhan = 1 ";
            if (ckbKhoNhap.Checked == true && cmbKhoNhap.Items.Count > 0)
            {
                dieuKienLoc += " AND dbo.tbl_Kho.MaKho = '" + cmbKhoNhap.SelectedValue.ToString() + "'";
            }
            if (ckbKhachHang.Checked == true && cmbKhachHang.Items.Count > 0)
            {
                dieuKienLoc += " AND dbo.tbl_DoiTac.MaDoiTac = '" + cmbKhachHang.SelectedValue.ToString() + "'";
            }
            if (ckbCongTrinh.Checked == true && cmbCongTrinh.Items.Count > 0)
            {
                dieuKienLoc += " AND dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn = '" + cmbCongTrinh.SelectedValue.ToString() + "'";
            }
            if (ckbNhaCungCap.Checked == true && cmbNhaCungCap.Items.Count > 0)
            {
                dieuKienLoc += " AND dbo.tbl_DoiTac.MaDoiTac = '" + cmbNhaCungCap.SelectedValue.ToString() + "'";
            }

            // Dieu kien loc ngay thang
            if (rbtnLocThang.Checked == true && cmbThang.Text != "")
            {
                dieuKienLoc += " AND MONTH(dbo.tbl_PhieuNhapKho.NgayNhap) = " + cmbThang.Text + " AND YEAR(dbo.tbl_PhieuNhapKho.NgayNhap) = " + nudNamThang.Value.ToString();
            }
            else if (rbtnLocQuy.Checked == true)
            {
                if (cmbQuy.Text == "1")
                {
                    dieuKienLoc += " AND dbo.tbl_PhieuNhapKho.NgayNhap BETWEEN '" + nudNamQuy.Value.ToString() + "-01-01" + "' AND '" + nudNamQuy.Value.ToString() + "-03-31'";
                }
                else if (cmbQuy.Text == "2")
                {
                    dieuKienLoc += " AND dbo.tbl_PhieuNhapKho.NgayNhap BETWEEN '" + nudNamQuy.Value.ToString() + "-04-01" + "' AND '" + nudNamQuy.Value.ToString() + "-06-30'";
                }
                else if (cmbQuy.Text == "3")
                {
                    dieuKienLoc += " AND dbo.tbl_PhieuNhapKho.NgayNhap BETWEEN '" + nudNamQuy.Value.ToString() + "-07-01" + "' AND '" + nudNamQuy.Value.ToString() + "-09-30'";
                }
                else if (cmbQuy.Text == "4")
                {
                    dieuKienLoc += " AND dbo.tbl_PhieuNhapKho.NgayNhap BETWEEN '" + nudNamQuy.Value.ToString() + "-10-01" + "' AND '" + nudNamQuy.Value.ToString() + "-12-31'";
                }
            }
            else if (rbtnLocNgay.Checked == true)
            {
                dieuKienLoc += " AND dbo.tbl_PhieuNhapKho.NgayNhap BETWEEN '" + dtipTuNgay.Value.ToString("yyyy-MM-dd") + "' AND '" + dtipDenNgay.Value.ToString("yyyy-MM-dd") + "'";
            }
            return dieuKienLoc;
        }

        private void btnLoc_Click(object sender, EventArgs e)
        {
            load_grid_view(XacDinhDieuKienLoc());
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            frmInBaoCaoNhapKho frm = new frmInBaoCaoNhapKho();
            string dieuKien = XacDinhDieuKienLoc();
            string sql = @"
                    SELECT    ROW_NUMBER() over(order by dbo.tbl_PhieuNhapKho.SoPhieu) as STT,dbo.tbl_PhieuNhapKho.SoPhieu as PhieuNhap, dbo.tbl_PhieuNhapKho.NgayNhap as NgayLap, SUM(dbo.tbl_ChiTietPhieuNhap.SoLuong) as SoLuong, dbo.tbl_LoaiPhieu.TenLoaiPhieu as KieuNhap
                    FROM         dbo.tbl_PhieuNhapKho INNER JOIN
                                          dbo.tbl_ChiTietPhieuNhap ON dbo.tbl_PhieuNhapKho.SoPhieu = dbo.tbl_ChiTietPhieuNhap.SoPhieu INNER JOIN
                                          dbo.tbl_LoaiPhieu ON dbo.tbl_PhieuNhapKho.MaLoaiPhieu = dbo.tbl_LoaiPhieu.MaLoaiPhieu INNER JOIN
                                          dbo.tbl_Kho ON dbo.tbl_PhieuNhapKho.MaKho = dbo.tbl_Kho.MaKho INNER JOIN
                                          dbo.tbl_DoiTac ON dbo.tbl_PhieuNhapKho.MaDoiTac = dbo.tbl_DoiTac.MaDoiTac INNER JOIN
                                          dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuNhapKho.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn
                    "
                    + dieuKien + @"
                    GROUP BY dbo.tbl_PhieuNhapKho.SoPhieu, dbo.tbl_PhieuNhapKho.NgayNhap, dbo.tbl_LoaiPhieu.TenLoaiPhieu
                   ";
            frm.BaoCaoNhapKhoBindingSource.DataSource = cn.getTable(sql);
            frm.ShowDialog();
        }

        private void ckbKhoNhap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhoNhap.Checked == true)
            {
                cmbKhoNhap.Enabled = true;
            }
            else
            {
                cmbKhoNhap.Enabled = false;
            }
        }

        private void ckbKhachHang_CheckedChanged_1(object sender, EventArgs e)
        {
            if (ckbKhachHang.Checked == true)
            {
                cmbKhachHang.Enabled = true;
                ckbNhaCungCap.Enabled = false;
            }
            else
            {
                cmbKhachHang.Enabled = false;
                ckbNhaCungCap.Enabled = true;
            }
        }

        private void ckbNhaCungCap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNhaCungCap.Checked == true)
            {
                cmbNhaCungCap.Enabled = true;
                ckbKhachHang.Enabled = false;
            }
            else
            {
                cmbNhaCungCap.Enabled = false;
                ckbKhachHang.Enabled = true;
            }
        }

        private void rbtnLocThang_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocThang.Checked == true)
            {
                cmbThang.Enabled = true;
                nudNamThang.Enabled = true;
            }
            else
            {
                cmbThang.Enabled = false;
                nudNamThang.Enabled = false;
            }
        }

        private void rbtnLocQuy_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocQuy.Checked == true)
            {
                cmbQuy.Enabled = true;
                nudNamQuy.Enabled = true;
            }
            else
            {
                cmbQuy.Enabled = false;
                nudNamQuy.Enabled = false;
            }
        }

        private void rbtnLocNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnLocNgay.Checked == true)
            {
                dtipTuNgay.Enabled = true;
                dtipDenNgay.Enabled = true;
            }
            else
            {
                dtipTuNgay.Enabled = false;
                dtipDenNgay.Enabled = false;
            }
        }

        private void ckbCongTrinh_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbCongTrinh.Checked == true)
            {
                cmbCongTrinh.Enabled = true;
            }
            else
            {
                cmbCongTrinh.Enabled = false;
            }
        }

        private void groupPanel1_Click(object sender, EventArgs e)
        {

        }

    }
}
